package com.uniledger.exchange.bank;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.uniledger.commonlib.util.DisplayUtil;
import com.uniledger.exchange.R;
import com.uniledger.exchange.base.activity.BaseActivity;
import com.uniledger.exchange.model.HttpResult;
import com.uniledger.exchange.model.StellarList;
import com.uniledger.exchange.model.api.Api;
import com.uniledger.exchange.model.rx.RxException;
import com.uniledger.exchange.model.rx.RxProgress;
import com.uniledger.exchange.model.rx.RxSchedulers;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import io.reactivex.functions.Consumer;
import me.yokeyword.indexablerv.EntityWrapper;
import me.yokeyword.indexablerv.IndexableAdapter;
import me.yokeyword.indexablerv.IndexableLayout;
import me.yokeyword.indexablerv.SimpleHeaderAdapter;

public class MangerBankActivity extends BaseActivity {
    @BindView(R.id.recycler_bank_list)
    IndexableLayout recyclerCard;

    BankCardIndexAdapter cardAdapter;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, MangerBankActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manger_bank);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerCard.setLayoutManager(manager);

        HorizontalDividerItemDecoration decoration = new HorizontalDividerItemDecoration.Builder(this)
                .color(Color.TRANSPARENT)
                .size(DisplayUtil.dip2px(this, 16))
                .showLastDivider()
                .build();

        recyclerCard.getRecyclerView().addItemDecoration(decoration);

        cardAdapter = new BankCardIndexAdapter(this);

        recyclerCard.setAdapter(cardAdapter);
        recyclerCard.setOverlayStyle_MaterialDesign(Color.RED);
        recyclerCard.setCompareMode(IndexableLayout.MODE_ALL_LETTERS);

        Api.getService().getBankcardList(1, 20)
                .compose(RxSchedulers.io_main())
                .compose(RxProgress.applyProgressBar(this))
                .subscribe(new Consumer<HttpResult<StellarList>>() {
                    @Override
                    public void accept(HttpResult<StellarList> stellarListHttpResult) throws Exception {

                    }
                }, new RxException<>(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                }));


        Api.getService().getBankcardList(1, 20)
                .compose(RxSchedulers.io_main())
                .compose(RxProgress.applyProgressBar(this))
                .subscribe(new Consumer<HttpResult<StellarList>>() {
                    @Override
                    public void accept(HttpResult<StellarList> stellarListHttpResult) throws Exception {

                        // 迪拜
                        //recyclerCard.addHeaderAdapter(new SimpleHeaderAdapter<>(cardAdapter, "迪", "迪拜", BankCard.getBankList()));

                        // 菲律宾
                        //recyclerCard.addHeaderAdapter(new SimpleHeaderAdapter<>(cardAdapter, "菲", "Philipin", BankCard.getBankList()));

                        cardAdapter.setDatas(stellarListHttpResult.data.bankcards, new IndexableAdapter.IndexCallback<BankCard>() {
                            @Override
                            public void onFinished(List<EntityWrapper<BankCard>> datas) {
                                // 数据处理完成后回调
                            }
                        });
                    }
                }, new RxException<>(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                }));

    }

    @BindString(R.string.card_bar_title)
    String title;

    @Override
    public void initBar() {
        super.initBar();
        toolbar.setTitle(title);
    }

    @Override
    public void getParams() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_manger_card, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.manger_card_add) {
            AddBankActivity.start(MangerBankActivity.this);
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AddBankActivity.MANGER_CARD_CODE && resultCode == RESULT_OK) {
            //TODO 绑定银行卡成功
        }
    }
}
